How to use <br> Tag In Javascript
For basic line breaks, you can directly add <br> to your text easily.But for larger projects or when you need to add multiple line breaks, it is convenient to use more efficient methods.
Example: This illustrates a div element with two lines of text, separated by a line break, using JavaScript’s innerHTML property.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="myElement"></div>
<script>
let element = document.getElementById('myElement');
element.innerHTML = 'lineone<br>linetwo';
</script>
</body>
</html>
Output:
How to Make a Breakline in JavaScript for innerHTML?
In JavaScript, while working with HTML you will frequently need to add line breaks whether building a complex or simple web application. In this article, you will learn to insert line breaks in JavaScript’s innerHTML.
These are the following approaches:
Table of Content
- Using < br > Tag
- Using Template Literals
- By Creating Elements
- Using CSS
Contact Us